﻿using Common.Device.Enums;
using Common.Device.Interfaces;
using Common.Device.ViewModels;
using Common.Tool.Models;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Common.Device.Models
{
    public class JueYuanNaiYa : Device
    {
        //测试模式
        public AnGuiYiEnum.TestMode mode { get; set; }
        public double 电压值 { get; set; }
        public double 电流值 { get; set; }
        public double 上升时间 { get; set; }
        public double 下降时间 { get; set; }
        public double 测试时间 { get; set; }
        public int 电压频率 { get; set; } = 50;
        public double 电弧值 { get; set; }
        /// <summary>
        /// 获取测试指令
        /// </summary>
        /// <param name="deviceName">仪器名</param>
        /// <returns></returns>
        public override Order GetOrder(string deviceName)
        {
            //测试指令
            Order order = new Order();
            order.temp = mode.ToString();
            order.name = deviceName;
            switch (mode)
            {
                case AnGuiYiEnum.TestMode.绝缘电阻: 
                    order.range = GetRange("MΩ");
                    order.deviceModel = JsonConvert.SerializeObject(new { 电压值, 上升时间, 测试时间, 下降时间, 上限值, 下限值 });
                    break;
                case AnGuiYiEnum.TestMode.直流耐压:
                    order.range = GetRange("mA");
                    order.deviceModel = JsonConvert.SerializeObject(new { 电压值, 电弧值, 上升时间, 测试时间, 下降时间, 上限值, 下限值 });
                    break;
                case AnGuiYiEnum.TestMode.交流耐压:
                    order.range = GetRange("mA");
                    order.deviceModel = JsonConvert.SerializeObject(new { 电压值, 电弧值, 上升时间, 测试时间, 下降时间, 上限值, 下限值 });
                    break;
                case AnGuiYiEnum.TestMode.漏电流:
                    order.range = GetRange("uA");
                    order.deviceModel = JsonConvert.SerializeObject(new { 电压值, 电流值, 上升时间, 测试时间, 下降时间, 上限值, 下限值 });
                    break;
                case AnGuiYiEnum.TestMode.停止测试:
                    order.deviceModel = JsonConvert.SerializeObject(new { });
                    break;
                default:
                    break;
            }
            return order;
        }
        /// <summary>
        /// 测试并返回测试结果
        /// </summary>
        /// <param name="dev">仪器实体</param>
        /// <param name="testOrder">测试项</param>
        /// <returns></returns>
        public override DeviceResult GetResult(object dev, TestOrder testOrder)
        {
            var device = dev as IJueYuanNaiYa;
            var result = new DeviceResult();
            switch (mode)
            {
                case AnGuiYiEnum.TestMode.绝缘电阻:
                case AnGuiYiEnum.TestMode.直流耐压:
                case AnGuiYiEnum.TestMode.交流耐压:
                case AnGuiYiEnum.TestMode.漏电流:
                    var r1 = device.SetTest(this);
                    result.success = r1.IsSucceed;
                    result.result = r1.IsSucceed ? r1.Value?.FirstOrDefault()?.value : r1.Err;
                    break;
                case AnGuiYiEnum.TestMode.停止测试:
                    var r2 = device.StopTest();
                    result.success = r2.IsSucceed;
                    result.result = r2.IsSucceed ? "OK" : r2.Err;
                    break;
                default:
                    break;
            }
            return result;
        }
    }
    /// <summary>
    /// 绝缘耐压测试结果
    /// </summary>
    public class JueYuanNaiYaResult
    {
        public string type { get; set; }
        public string value { get; set; }
        public bool isPass { get; set; }
    }
}
